class File():
    def __init__(self,filename,mode):
        self.filename=filename
        self.mode=mode


    def __enter__(self):
        print('enter')
        self.f=open(self.filename,self.mode)
        print(self.f)
        return self.f

    def __exit__(self, exc_type, exc_val, exc_tb):
        print(self, exc_type, exc_val, exc_tb)
        print('exit')
        self.f.close()

with File('默写.txt','w') as f:
    print(f.__dict__)
    print('开始写入',f,File('默写.txt','w'))
    f.write('默写成功')








